package com.yutel.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class HttpConnect {

	public static void main(String[] args) {
		HttpConnect hc = new HttpConnect();
		hc.detailXml("http://192.168.3.235/epg/videos/v9304.xml");
//		hc.updateinfo();
	}

	public void detailXml(String url) {
		HttpConnect hc = new HttpConnect();
		long start = System.currentTimeMillis();
		HttpURLConnection conn = hc.get(url);
		conn.setConnectTimeout(1000);
		try {
			if (conn.getResponseCode() == 200) {
				long step = System.currentTimeMillis();
				long timestep = step - start;
				System.out.println("timestep=" + timestep);
				InputStream inputS = conn.getInputStream();
				BufferedReader br=new BufferedReader(new InputStreamReader(inputS));
				String info=null;
				while((info=br.readLine())!=null) {
					System.out.println(info);
				}
				inputS.close();
			} else {
				System.err.println("responseCode=" + conn.getResponseCode());
			}
			long stop = System.currentTimeMillis();
			long timelan = stop - start;
			System.out.println("alltime=" + timelan);
		} catch (IOException e) {
			long error = System.currentTimeMillis();
			long errortime = error - start;
			System.out.println("errortime=" + errortime);
			e.printStackTrace();
		}
	}

	public void updateinfo() {
		HttpConnect hc = new HttpConnect();
		long start = System.currentTimeMillis();
		HttpURLConnection conn = hc
				.get("http://epg.funhd.cn/epg/updateinfo.txt");
		conn.setConnectTimeout(1000);
		try {
			if (conn.getResponseCode() == 200) {
				long step = System.currentTimeMillis();
				long timestep = step - start;
				System.out.println("timestep=" + timestep);
				InputStream inputS = conn.getInputStream();
				BufferedReader br=new BufferedReader(new InputStreamReader(inputS));
				String info =br.readLine();
				inputS.close();
				System.out.println("updateinfo=" + info);
			}
			long stop = System.currentTimeMillis();
			long timelan = stop - start;
			System.out.println("alltime=" + timelan);
		} catch (IOException e) {
			long error = System.currentTimeMillis();
			long errortime = error - start;
			System.out.println("errortime=" + errortime);
			e.printStackTrace();
		}
	}

	private HttpURLConnection get(String url) {
		try {
			URL curl = new URL(url);
			HttpURLConnection connection = (HttpURLConnection) curl
					.openConnection();
			connection.setRequestMethod("GET");
			return connection;
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
